package cn.edu.nit.sys.service.impl;

import cn.edu.nit.sys.entity.Notice;
import cn.edu.nit.sys.entity.User;
import cn.edu.nit.sys.mapper.NoticeMapper;
import cn.edu.nit.sys.mapper.UserMapper;
import cn.edu.nit.sys.service.NoticeService;
import cn.edu.nit.sys.socket.MessageSocketHandler;
import cn.edu.nit.sys.utils.R;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author xiaobai
 * @Date 2022/10/30 21:35
 * @Version 1.0.0
 */
@Service
@Slf4j
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public R getNoticeList(String title, String start, String end, Page<Notice> page) {
        Page<Notice> noticeList = baseMapper.getNoticeList(title, start, end, page);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", noticeList.getTotal());
        jsonObject.put("list", noticeList.getRecords());
        return R.ok(jsonObject);
    }

    @Override
    public R addNotice(Notice notice) {
        String[] targets = notice.getTarget().split(",");
        List<String> roles = baseMapper.getRoles(targets);
        notice.setTarget(roles.toString());
        User user = userMapper.selectById(notice.getSubId());
        boolean toAll = false;
        boolean toSelf = false;
        if(baseMapper.insert(notice) > 0) {
            // for (int i = 0; i < targets.length; i++) {
            //     redisTemplate.opsForValue().set(targets[i], uuid);
            //     if(targets[i].equalsIgnoreCase(user.getRoleId().toString())) {
            //         toSelf = true;
            //     }
            //     if("0".equalsIgnoreCase(targets[i])) {
            //         toAll = true;
            //     }
            // }
            try {
                if(toAll || toSelf) {
                    // messageSocketHandler.sendNotice(notice.getTitle());
                }else {

                }
            } catch (Exception e) {
                log.error("异常{}", e);
                return R.error("添加失败");
            }
            return R.ok("添加成功");
        }
        return R.error("添加失败");
    }

    @Override
    public R getDetail(String id) {
        Notice notice = baseMapper.selectById(id);
        return notice != null ? R.ok(notice) : R.error("获取详情失败");
    }

    @Override
    public R updateNotice(Notice notice) {
        return baseMapper.updateById(notice) > 0 ? R.ok("修改成功") : R.error("修改失败");
    }

    @Override
    public R delNotice(String id) {
        return baseMapper.deleteById(id) > 0 ? R.ok("删除成功") : R.error("删除失败");
    }
}
